var com = {
	mahees : {
		jobTracker : {
			EventBus : function() {
				return _.extend({}, Backbone.Events);
			},
			views : {},
			models : {},
			collections : {}
		},
		backbone : {
			View : {},
			Models : {},
			Collections : {},
			Utility : {}
		}
	}
};

_.templateSettings = {
	evaluate : /\<\?([\s\S]+?)\?\>/g,
	interpolate : /\<\?=([\s\S]+?)\?\>/g,
	escape : /\<\?-([\s\S]+?)\?\>/g
};

// _.templateSettings = {
// interpolate : /\<\?\=(.+?)\?\>/g,
// evaluate : /\{?(.+?)?\}/g
// };

com.mahees.backbone.View.BasicView = Backbone.View.extend({

	initialize : function() {
		this.beforeInitialize && this.beforeInitialize();
		
		_.bindAll(this, 'render', 'unrender', 'destroy', 'updateModel', 'errorHandler');

		this.model.bind('change', this.render);
		this.model.bind('destroy', this.unrender);
		this.model.bind('error', this.errorHandler);

		this.afterInitialize && this.afterInitialize();
	},

	updateModel : function(e) {
		com.mahees.backbone.Utility.FieldChanged(this.model, e, this.el);
	},

	errorHandler : function(model, errors) {
		for ( var name in errors) {
			$('[name=' + name + ']', this.el).addClass('error').focus();
		}
	},

	unrender : function() {
		$(this.el).remove();
	},

	destroy : function(e) {
		e.preventDefault();
		this.model.destroy();
		return false;
	},

	render : function() {
		this.beforeRender && this.beforeRender();

		var html = _.template($(this.templateIdentifier).html(), this.model.toJSON())
		$(this.el).html(html);
		
		this.afterRender && this.afterRender();
		
		return this;
	},
});

com.mahees.backbone.View.BasicListView = Backbone.View.extend({
	initialize : function() {
		this.beforeInitialize && this.beforeInitialize();
		
		_.bindAll(this, 'render', 'appendItem', 'addItem');

		this.collection.bind('reset', this.render);
		this.collection.bind('add', this.appendItem);

		this.counter = 0;
		
		this.afterInitialize && this.afterInitialize();
	},

	render : function() {
		$(this.el).empty();
		this.counter = 0;

		this.beforeRender && this.beforeRender();

		_(this.collection.models).each(function(item) {
			this.appendItem(item);
		}, this);
		
		this.afterRender && this.afterRender();

		return this;
	},

	appendItem : function(model) {
		this.counter++;
		model.set({
			counter : this.counter
		}, {
			silent : true
		});
		var view = new this.refModelView({
			model : model,
			eventBus : this.options.eventBus
		});
		$(this.el).append(view.render().el);
	},

	addItem : function(e) {
		e.preventDefault();
		var model = new this.collection.model;
		this.collection.add(model);
		return false;
	},
});
// End generic stuff

// Utilities
com.mahees.backbone.Utility.FieldChanged = function(model, e, el) {
	console.log(e);
	var obj = {};
	
	switch (e.target.type) {
	case 'text':
		//var searchStr = "[name='" + e.target.name + "']";
		obj[e.target.name] = e.target.value;
		break;
	case 'radio':
		obj[e.target.dataset.name] = e.target.dataset.value
		break;
	default:
	}

	model.set(obj);
}

// com.mahees.backbone.Utility.ErrorHandler = function(className, errorObj, el)
// {
// var searchStr = className + "[name='" + errorObj.name + "']";
// var node = $(searchStr, el);
// node.addClass('error');
// node.focus();
// }

com.mahees.backbone.Utility.ResetCollection = function(model, attr) {
	var collection = model.previous(attr)
	
	collection.reset();
	_.each(model.get(attr), function(jsonObj) {
		collection.add(new collection.model(jsonObj));
	});
	
	var setObj = {};
	setObj[attr] = collection;

	model.set(setObj, {
		silent : true
	});
}

com.mahees.backbone.Utility.ConvertArrayToCollection = function(jsonObject, attr, refCollFunc) {
	var collection = new refCollFunc();
	
	collection.reset();
	_.each(jsonObject[attr], function(jsonObj) {
		collection.add(new collection.model(jsonObj));
	});

	jsonObject[attr] = collection;
}

com.mahees.backbone.Utility.SelectItem = function(model, attr, el) {
	$("select[name='" + attr + "']", el).val(model.get(attr));
}